<template>
  <div :ref="ref" :style="{width: '100%', height: '100%' }" />
</template>

<script>
import echarts from 'echarts'
export default {
  name: 'AssetStatusChart',
  props: {
    data: {
      required: true,
      type: Array,
      default: function() {
        return []
      }
    },
    title: {
      required: true,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ref: 'assetStatusChart' + Math.floor(Math.random() * 1000000)
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    init() {
      const that = this
      const stChart = echarts.init(this.$refs[this.ref])
      const yAxisData = this.data.map(it => it.name)
      const seriesData = this.data.map(it => it.value)
      const option = {
        title: {
          text: that.title
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          y: 40,
          left: 15,
          right: 40,
          bottom: 15,
          containLabel: true
        },
        xAxis: {
          type: 'value',
          splitLine: { // 网格线
            show: true // 隐藏或显示
          },
          axisLabel: {
            textStyle: {
              color: '#B6B4B7',
              fontSize: 12,
              fontWeight: '400'
            }
          }
        },
        yAxis: {
          type: 'category',
          splitLine: { // 网格线
            show: false, // 隐藏或显示
            lineStyle: {
              color: ['#262841']
            }
          },
          axisLabel: {
            textStyle: {
              color: '#B6B4B7',
              fontSize: 12,
              fontWeight: '400'
            }
          },
          data: yAxisData
        },
        series: [
          {
            type: 'bar',
            barWidth: 10,
            stack: '总量',
            label: {
              show: true,
              position: 'right'
            },
            itemStyle: {
              normal: {
                color: '#0ECED1'
              }
            },
            data: seriesData
          }
        ]
      }
      stChart.setOption(option)
    }
  }
}
</script>

<style>
</style>
